using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FiltronASP.Models;
using FiltronASP.Models.DAO.WIX;
using FiltronASP.Models.DAO.Extensions;
using FiltronASP.Models.DAO.Interfaces.WIX;
using FiltronASP.Models.DAO.Filtron;
using FiltronASP.Models.DAO.Interfaces.Filtron;
using FiltronASP.Helpers.Translations;
using FiltronASP.Helpers.CatalogueHelpers;


namespace FiltronASP.Helpers.AjaxModel
{
    /// <summary>
    /// Klasa pomocnicza do generowania stringa z lista modeli.
    /// </summary>
    public static class truckModelHelper
    {

        /// <summary>
        /// Metoda generujaca string do comba na podstawie id marki.
        /// </summary>
        /// <param name="makeId">marka</param>
        /// <returns>string</returns>
        public static string GetModelComboString(int makeId, string languageName)
        {
            string comboString = String.Empty;
            comboString = translationHelper.getTranslation("_selectModel", languageName) + ",-1;";
            IWixModelDAO wixModelDAO = new WixModelDAO();
            if (makeId > 0)
            {
                IQueryable<WixModel> wixModels = wixModelDAO.FindByMake(makeId);
                foreach (WixModel model in wixModels)
                {
                    comboString += model.model_name;
                    comboString += ",";
                    comboString += model.model_id;
                    comboString += ";";
                }   //foreach
            }   //if
            return comboString;
        }

        /// <summary>
        /// Metoda generujaca diva leftSearchSide do silnikow na podstawie id modelu.
        /// </summary>
        /// <param name="modelId">model</param>
        /// <returns>string</returns>
        public static string GetEngineDivString(int modelId, string languageName)
        {
            if (modelId.Equals(0))
            {
                return null;
            }
            if (languageName == String.Empty)
            {
                return null;
            }

            return Helpers.AjaxModel.EngineTableHelperWIX.generateEngineTable(modelId, languageName);
        }

        /// <summary>
        /// Metoda generujaca diva leftSearchSide do silnikow.
        /// </summary>
        /// <returns>string</returns>
        public static string GetEngineDivString()
        {
            string htmlToReturn = "";
            htmlToReturn += "            <h3>Wybierz silnik</h3>";
            htmlToReturn += "            <table>";
            htmlToReturn += "                <thead>";
            htmlToReturn += "                    <td class=\"engine\">Silnik</td>";
            htmlToReturn += "                    <td class=\"cc\">CC <img src=\"../../../Content/images/engine_cc.jpg\" alt=\"cc\"></td>";
            htmlToReturn += "                    <td class=\"no\">No <img src=\"../../../Content/images/engine_no.jpg\" alt=\"no\"></td>";
            htmlToReturn += "                    <td class=\"hp\">HP <img src=\"../../../Content/images/engine_hp.jpg\" alt=\"hp\"></td>";
            htmlToReturn += "                </thead>";
            htmlToReturn += "                <tbody>";
            htmlToReturn += "                </tbody>";
            htmlToReturn += "            </table>";
            return htmlToReturn;
        }

        /// <summary>
        /// Metoda generująca listę marek na podstawie typu pojazdu. (vehicle)
        /// </summary>
        /// <param name="vehicleId">typ pojazdu</param>
        /// <returns>string</returns>
        public static string getBrandDropDownByVehicleId(int vehicleId)
        {
            string comboString = "--- wybierz producenta ---,-1;";
            IWixMakeDAO wixMakesDAO = new WixMakeDAO();
            if (vehicleId > 0) {
                IQueryable<WixMake> wixMakes = wixMakesDAO.FindByVehicle(vehicleId);
                foreach (WixMake make in wixMakes) {
                    comboString += make.make_name;
                    comboString += ",";
                    comboString += make.make_id;
                    comboString += ";";
                }
            }

            return comboString;
        }

        public static string GetFiltersByEngineAndModel(int engineId, int modelId, string languageName)
        {
            string filtersToReturn = "";
            IWixFilterDAO wixFilterDAO = new WixFilterDAO();
            IWixEngineDAO wixEngineDAO = new WixEngineDAO();
            IWixModelDAO wixModelDAO = new WixModelDAO();

            string engineName = wixEngineDAO.FindByPK(engineId).engine_name;
            int engineYearFrom = Convert.ToInt32(wixModelDAO.FindByPK(modelId).model_year_from);
            int engineYearTo = Convert.ToInt32(wixModelDAO.FindByPK(modelId).model_year_to);
            
                filtersToReturn += "<div class=\"\" id=\"\">";
                if ((engineYearFrom.Equals(0)) || (engineYearTo.Equals(0)))
                {
                    filtersToReturn += "    <h3>" + translationHelper.getTranslation("_filterList", languageName) + " <span>" + engineName + "</span></h3>";
                }
                else {
                    filtersToReturn += "    <h3>" + translationHelper.getTranslation("_filterList", languageName) + " <span>" + engineName + " <strong>(" + engineYearFrom + "-" + engineYearTo + ")</strong></span></h3>";
                }
                filtersToReturn += "    <table id=\"filtersTable\">";
                filtersToReturn += "        <thead>";
                filtersToReturn += "            <tr>";
                filtersToReturn += "                <td class=\"year\"><span style=\"display: block; width: 85px; float: left; height: 45px; line-height: 45px;\">" + translationHelper.getTranslation("_hlpDescription", languageName) + "</span> </td>";
                filtersToReturn += "                <td class=\"oil\"><span style=\"display: block; width: 50px; float: left; height: 45px; line-height: 45px;\">" + translationHelper.getTranslation("_hlpOil", languageName) + "</span> <img src=\"../../../Content/images/filter_oil.png\" alt=\"no\"></td>";
                filtersToReturn += "                <td class=\"fuel\"><span style=\"display: block; width: 50px; float: left; height: 45px; line-height: 45px;\">" + translationHelper.getTranslation("_hlpFuel", languageName) + "</span> <img src=\"../../../Content/images/filter_fuel.png\" alt=\"hp\"></td>";
                filtersToReturn += "                <td class=\"air\"><span style=\"display: block; width: 50px; float: left; height: 45px; line-height: 45px;\">" + translationHelper.getTranslation("_hlpAir", languageName) + "</span> <img src=\"../../../Content/images/filter_air.png\" alt=\"hp\"></td>";
                filtersToReturn += "                <td class=\"cabine\"><span style=\"float: left; padding-top: 15px; width: 60px;\">" + translationHelper.getTranslation("_hlpCabin", languageName) + "</span><img src=\"../../../Content/images/filter_cabine.png\" alt=\"cc\"></td>";
                filtersToReturn += "            </tr>";
                filtersToReturn += "        </thead>";
                filtersToReturn += "        <tbody>";

            if (engineId > 0) {
                IQueryable<WixFilter> wixFilters = wixFilterDAO.FindByEngineAndModel(engineId, modelId);
                if (wixFilters != null)
                {
                    string engineDescription = wixEngineDAO.FindByPK(engineId).engine_description;
                    string oilFilter = "<li class=\"decornone\">---</li>";
                    string fuelFilter = "<li class=\"decornone\">---</li>";
                    string airFilter = "<li class=\"decornone\">---</li>";
                    string otherFilter = "<li class=\"decornone\">---</li>";

                    foreach (WixFilter filter in wixFilters)
                    {
                        int filterType = Convert.ToInt32(filter.filter_type_id);
                        if (filterType.Equals(40))
                        {
                            if (oilFilter.Equals("<li class=\"decornone\">---</li>"))
                            {
                                oilFilter = "<li onclick=\"truck_makeFilterActive(this, '"+ languageName +"')\" id=\"" + filter.filter_id + "\">" + filter.filter_number + "</li>";
                            }
                            else
                            {
                                oilFilter += "<li onclick=\"truck_makeFilterActive(this, '" + languageName + "')\" id=\"" + filter.filter_id + "\">" + filter.filter_number + "</li>";
                            }
                        }
                        else if (filterType.Equals(47))
                        {
                            if (fuelFilter.Equals("<li class=\"decornone\">---</li>"))
                            {
                                fuelFilter = "<li onclick=\"truck_makeFilterActive(this, '" + languageName + "')\" id=\"" + filter.filter_id + "\">" + filter.filter_number + "</li>";
                            }
                            else
                            {
                                fuelFilter += "<li onclick=\"truck_makeFilterActive(this, '" + languageName + "')\" id=\"" + filter.filter_id + "\">" + filter.filter_number + "</li>";
                            }
                        }
                        else if (filterType.Equals(48))
                        {
                            if (airFilter.Equals("<li class=\"decornone\">---</li>"))
                            {
                                airFilter = "<li onclick=\"truck_makeFilterActive(this, '" + languageName + "')\" id=\"" + filter.filter_id + "\">" + filter.filter_number + "</li>";
                            }
                            else
                            {
                                airFilter += "<li onclick=\"truck_makeFilterActive(this, '" + languageName + "')\" id=\"" + filter.filter_id + "\">" + filter.filter_number + "</li>";
                            }
                        }
                        else
                        {
                            if (otherFilter.Equals("<li class=\"decornone\">---</li>"))
                            {
                                otherFilter = "<li onclick=\"truck_makeFilterActive(this, '" + languageName + "')\" id=\"" + filter.filter_id + "\">" + filter.filter_number + "</li>";
                            }
                            else
                            {
                                otherFilter += "<li onclick=\"truck_makeFilterActive(this, '" + languageName + "')\" id=\"" + filter.filter_id + "\">" + filter.filter_number + "</li>";
                            }
                        }
                    }


                    filtersToReturn += "<tr id=\"\">";
                    if (engineDescription == String.Empty)
                    {
                        filtersToReturn += "    <td id=\"\"><ul><li> --- </li></ul></td>";
                    }
                    else
                    {
                        filtersToReturn += "    <td id=\"\"><ul><li>" + engineDescription + "</li></ul></td>";
                    }
                    filtersToReturn += "    <td id=\"\"><ul>" + oilFilter + "</ul></td>";
                    filtersToReturn += "    <td id=\"\"><ul>" + fuelFilter + "</ul></td>";
                    filtersToReturn += "    <td id=\"\"><ul>" + airFilter + "</ul></td>";
                    filtersToReturn += "    <td id=\"\"><ul>" + otherFilter + "</ul></td>";
                    filtersToReturn += "</tr>";
                }
                else {
                    filtersToReturn += "<tr><td><ul><li> --- </li></ul></td></tr>";
                }
            }
            filtersToReturn += "        </tbody>";
            filtersToReturn += "    </table>";
            filtersToReturn += "</div>";

            return filtersToReturn;
        }

        public static string generateSingleFilter(int filterId, string languageName) { 
            string htmlToReturn = "";

            IWixFilterDAO wixFilterDAO = new WixFilterDAO();
            WixFilter filter = wixFilterDAO.FindByPK(filterId);
            
            htmlToReturn += "<div class=\"searchRightSideTop\" id=\"searchRightSideTop\">";
            htmlToReturn += "                <div id=\"searchRightSideTopLeft\">";
            htmlToReturn += "                    <h3>" + translationHelper.getTranslation("_filter", languageName) + " <span>" + filter.filter_number + "</span></h3>";
            htmlToReturn += "                    <div class=\"searchRightSideTopLeftDimensions\">";
            htmlToReturn += "                        <strong>" + translationHelper.getTranslation("_hlpSizes", languageName) + " (mm):</strong>";
            htmlToReturn += "                        <table>";
            if (filter.filter_outside_dia_top > 0)
            {
                htmlToReturn += "                            <tr><td style=\"width: 290px;\">" + translationHelper.getTranslation("_outerDiamaterTop", languageName) + ": <span>" + SingleFilterHelper.parseFilterSizes(SingleFilterHelper.convertInchesToMetric(filter.filter_outside_dia_top)) + "</span></td></tr>";
            }
            else {
                htmlToReturn += "                            <tr><td style=\"width: 290px;\">" + translationHelper.getTranslation("_outerDiamaterTop", languageName) + ": <span> --- </span></td></tr>";
            }

            if (filter.filter_outside_dia_bottom > 0)
            {
                htmlToReturn += "                            <tr><td style=\"width: 290px;\">" + translationHelper.getTranslation("_outerDiamaterBottom", languageName) + ": <span>" + SingleFilterHelper.parseFilterSizes(SingleFilterHelper.convertInchesToMetric(filter.filter_outside_dia_bottom)) + "</span></td></tr>";
            }
            else {
                htmlToReturn += "                            <tr><td style=\"width: 290px;\">" + translationHelper.getTranslation("_outerDiamaterBottom", languageName) + ": <span> --- </span></td></tr>";
            }
            if (filter.filter_inside_dia_top > 0)
            {
                htmlToReturn += "                            <tr><td style=\"width: 290px;\">" + translationHelper.getTranslation("_innerDiameter", languageName) + ": <span>" + SingleFilterHelper.parseFilterSizes(SingleFilterHelper.convertInchesToMetric(filter.filter_inside_dia_top)) + "</span></td></tr>";
            }
            else {
                htmlToReturn += "                            <tr><td style=\"width: 290px;\">" + translationHelper.getTranslation("_innerDiameter", languageName) + ": <span> --- </span></td></tr>";
            }
            if (filter.filter_height > 0)
            {
                htmlToReturn += "                            <tr><td style=\"width: 290px;\">" + translationHelper.getTranslation("_height", languageName) + ": <span>" + SingleFilterHelper.parseFilterSizes(SingleFilterHelper.convertInchesToMetric(filter.filter_height)) + "</span></td></tr>";
            }
            else
            {
                htmlToReturn += "                            <tr><td style=\"width: 290px;\">" + translationHelper.getTranslation("_height", languageName) + ": <span> --- </span></td></tr>";
            }
            if (filter.filter_thread_size != null) {
                htmlToReturn += "                            <tr><td style=\"width: 290px;\">" + translationHelper.getTranslation("_threadSize", languageName) + ": <span>" + filter.filter_thread_size + "</span></td></tr>";
            }
            htmlToReturn += "                        </table>";
            htmlToReturn += "                    </div>";
            htmlToReturn += "                    <div class=\"searchRightSideTopLeftInfo\">";
            htmlToReturn += "                        <ul>";

            string filterStyle = "---";
            string filterType = "---";
            string filterMedia = "---";
            string filterService = "---";
            if ((filter.filter_style_id > 0) && (filter.filter_style_id != null))
            {
                filterStyle = SingleFilterHelper.getFilterStyle(Convert.ToInt32(filter.filter_style_id));
            }
            if ((filter.filter_type_id > 0) && (filter.filter_type_id != null))
            {
                filterType = SingleFilterHelper.getFilterType(Convert.ToInt32(filter.filter_type_id));
            }
            if ((filter.filter_media_id > 0) && (filter.filter_media_id != null))
            {
                filterMedia = SingleFilterHelper.getMediaType(Convert.ToInt32(filter.filter_media_id));
            }
            if ((filter.filter_service_id > 0) && (filter.filter_service_id != null))
            {
                filterMedia = SingleFilterHelper.getFilterService(Convert.ToInt32(filter.filter_service_id));
            }

            htmlToReturn += String.Format("<li>" + translationHelper.getTranslation("_hlpFilterStyle", languageName) + ": <span>{0}</span></li>", filterStyle);
            htmlToReturn += String.Format("<li>" + translationHelper.getTranslation("_hlpFilterType", languageName) + ": <span>{0}</span></li>", filterType);
            htmlToReturn += String.Format("<li>" + translationHelper.getTranslation("_hlpFilterMedia", languageName) + ": <span>{0}</span></li>", filterMedia);
            htmlToReturn += String.Format("<li>" + translationHelper.getTranslation("_hlpFilterService", languageName) + ": <span>{0}</span></li>", filterService);

            htmlToReturn += String.Format("<li>" + translationHelper.getTranslation("_hlpInstruction", languageName) + ": <span>{0}</span></li>", String.Empty);
            htmlToReturn += String.Format("<li>" + translationHelper.getTranslation("_hlpApplication", languageName) + ":<br/><span>{0}</span></li>", filter.filter_principle_applications);
            htmlToReturn += "                        </ul>";
            htmlToReturn += "                    </div>";
            htmlToReturn += "                    <div class=\"searchRightSideTopLeftCommands\">";
            htmlToReturn += "                        <img onclick=\"printFilterDescription()\" src=\"../../../Content/images/engine_printer.jpg\" alt=\"Printer\" /> <span onclick=\"printFilterDescription()\" class=\"desc_commands\">" + translationHelper.getTranslation("_hlpPrint", languageName) + "</span>";
            htmlToReturn += "                        <img onclick=\"sendFilterDescription()\" src=\"../../../Content/images/engine_mail.jpg\" alt=\"Mail\" /> <span onclick=\"sendFilterDescription()\" class=\"desc_commands\">" + translationHelper.getTranslation("_hlpSend", languageName) + "</span>";
            htmlToReturn += "                    </div>";
            htmlToReturn += "                </div>";
            htmlToReturn += "                <div id=\"searchRightSideTopRight\">";
            htmlToReturn += "                    <img src=\"../../../Content/images/temp/example_filter.png\" alt=\"Example\" />";
            htmlToReturn += "                    <a href=\"../../../Content/images/temp/example_filter.png\" rel=\"lightbox\"><span>" + translationHelper.getTranslation("_hlpZoom", languageName) + " <img src=\"../../../Content/images/engine_magn.png\" alt=\"Zoom\" /></a></span>";
            htmlToReturn += "                </div>";
            htmlToReturn += "            </div>";

            return htmlToReturn;
        }
    }
}
